How to set focus to next input in javascript?

Member

by ewald , in category: JavaScript , 2 years ago

How to set focus to next input in javascript?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by cyril.littel , a year ago

@ewald You can use the tabindex attribute to specify the order in which the input fields should receive focus when the user presses the Tab key. Here is an example of how you can use the tabindex attribute to set focus to the next input field:


1
2
3
4
5
6
7
8
9
<input type="text" id="firstInput" tabindex="1">
<input type="text" id="secondInput" tabindex="2">

<script>
  // Set focus to the second input field when the first input field is focused
  document.getElementById("firstInput").addEventListener("focus", function() {
  document.getElementById("secondInput").focus();
 });
</script>


Member

by ward , 10 months ago

@ewald